<template>
  <div>
  <el-row class="title-bar" v-if="childOrg.orgId>0">
    <span class="title-bar-span">基础信息</span>
    <el-button class="title-button" icon="el-icon-edit"  type="text" @click="handleUpdate" v-hasPermi="['labdata:platform:edit']"/>
  </el-row>
  <el-form ref="form" :model="form" :rules="rules" v-if="childOrg.orgId>0">
  <table class="tb_edit">
  <tr>
    <td class="td_label">名称:</td>
    <td class="td_text">
      <span  v-text="form.platformName"></span>
    </td>
    <td class="td_label">项目编码:</td>
    <td class="td_text">
      <span  v-text="form.projectCode"></span>
    </td>
    <td class="td_label">状态</td>
    <td class="td_text" v-text="this.selectDictLabel(this.orgStatusOptOptions,form.status)">

    </td>
  </tr>
  <tr>
    <td >等级:</td>
    <td >
      <span  v-text="selectDictLabel(platformLevelOptions,form.platformLevel)"></span>
    </td>
    <td >项目类型:</td>
    <td >
      <span  v-text="selectDictLabel(projectTypeOptions,form.projectType)"></span>
    </td>
    <td >网址:</td>
    <td >
      <span  v-text="form.webUrl"></span>
    </td>
  </tr>
  <tr>
    <td >所属学院:</td>
    <td ><span  v-text="getAcademyName(form.academyId)"></span>
    </td>
    <td >批准日期:</td>
    <td ><span  v-text="form.approvalDate"></span>
       </td>
    <td >批准文号:</td>
    <td >
      <span  v-text="form.approvalNo"></span>
    </td>
  </tr>
  <tr>
    <td class="td_label">省里文号:</td>
    <td class="td_text">
      <span  v-text="form.provinceCode"></span>
    </td>
    <td class="td_label">省里资金:</td>
    <td class="td_text">
      <span  v-text="form.provincePayed"></span>
    </td>
    <td class="td_label">校内文号:</td>
    <td class="td_text">
      <span  v-text="form.collegeCode"></span>
    </td>
    </td>
  </tr>
  <tr>
    <td class="td_label">校内资金:</td>
    <td class="td_text">
      <span  v-text="form.collegePayed"></span>
    </td>
    <td class="td_label">验收文号:</td>
    <td class="td_text">
      <span  v-text="form.acceptanceCode"></span>
    </td>
    <td class="td_label">验收情况:</td>
    <td class="td_text">
      <span  v-text="form.acceptanceMark"></span>
    </td>
    </td>
  </tr>
  </table>
  </el-form>
  <!-- 添加或修改实验平台信息对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="平台名称" prop="platformName">
            <el-input v-model="form.platformName" placeholder="请输入名称" size="small" class="input_w"/>
          </el-form-item>
        </el-col>
         <el-col :span="12">
          <el-form-item label="项目编码" prop="projectCode">
            <el-input v-model="form.projectCode" placeholder="请输入项目编码" class="input_w"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="等级" prop="platformLevel">
            <el-select v-model="form.platformLevel" placeholder="请选择等级" class="input_w">
              <el-option
                v-for="dict in platformLevelOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目类型" prop="projectType">
            <el-select v-model="form.projectType" placeholder="请选择项目类型" class="input_w">
              <el-option
                v-for="dict in projectTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="网址" prop="webUrl">
            <el-input v-model="form.webUrl" placeholder="请输入网址" class="input_w"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属学院" prop="academyId">
            <el-select v-model="form.academyId" placeholder="请选择学院" class="input_w">
              <el-option v-for="item in secondOrgList" :key="item.orgName" :label="item.orgName"
                :value="item.orgId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="批准日期" prop="approvalDate">
            <el-date-picker clearable size="small" v-model="form.approvalDate" type="date" value-format="yyyy-MM-dd"
              placeholder="选择填单日期" style="width: 200px;">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="批准文号" prop="approvalNo">
            <el-input v-model="form.approvalNo" placeholder="请输入批准文号" class="input_w"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="省里文号" prop="provinceCode">
            <el-input v-model="form.provinceCode" placeholder="请输入省里下达文号" class="input_w"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="省里资金" prop="provincePayed">
            <el-input v-model="form.provincePayed" placeholder="请输入省里下达资金" class="input_w"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="校内文号" prop="collegeCode">
            <el-input v-model="form.collegeCode" placeholder="请输入校内经费文号" class="input_w"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="校内资金" prop="collegePayed">
            <el-input v-model="form.collegePayed" placeholder="请输入校内经费下拨资金"class="input_w" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="验收文号" prop="acceptanceCode">
            <el-input v-model="form.acceptanceCode" placeholder="请输入验收文号" class="input_w"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="验收情况" prop="acceptanceMark">
            <el-input v-model="form.acceptanceMark" placeholder="请输入验收情况" class="input_w"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="下属机构" prop="subordinateList">
          <treeselect v-model="form.subordinateList" :options="deptOptions" :multiple="true" :default-expand-level="1"
          :show-count="true" placeholder="请选择下属机构"
            size="small"  style="width: 530px;"/>
        </el-form-item>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" v-hasPermi="['labdata:platform:edit']">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>
import { getPlatform,addPlatform, updatePlatform} from "@/api/labdata/platform";
import {getOrgByFunctionOpt} from "@/api/labdata/orgnization";
import {treeselect} from "@/api/labdata/orgnization";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "PlatformCom",
  components:{Treeselect},
  data() {
    return {
      // 部门树选项
      deptOptions: [],
      is_org_edit:false,
      currentOrg: {},
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 等级字典
      platformLevelOptions: [],
      // 项目类型字典
      projectTypeOptions: [],
      secondOrgList:[],
      orgStatusOptOptions: [],
      // 查询参数
      queryParams: {
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        platformName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        projectCode: [
          { required: true, message: "项目编码不能为空", trigger: "blur" }
        ],
        platformLevel: [
          { required: true, message: "等级不能为空", trigger: "change" }
        ],
        academyId: [
          { required: true, message: "所属学院不能为空", trigger: "blur" }
        ],
      }
    };
  },
  props: {
    childOrg: {
      type: Object,
      default: () => ({})
    },
  },
  watch: {
    childOrg: function(newVal, oldVal) {
      this.currentOrg = newVal;
      this.is_org_edit = false;
      this.getFormData();
    }

  },
  created() {
    this.getTreeselect();
    this.getDicts("platform_level_opt").then(response => {
      this.platformLevelOptions = response.data;
    });
    this.getDicts("platform_project_opt").then(response => {
      this.projectTypeOptions = response.data;
    });
    getOrgByFunctionOpt(2).then(response => {
      this.secondOrgList = response.data;
    });
    this.getDicts("sys_normal_disable").then(response => {
      this.orgStatusOptOptions = response.data;
    });
    if(this.childOrg){
      this.currentOrg = this.childOrg;
      this.getFormData();
    }
  },
  methods: {
    getAcademyName(id){
      if(!id){
        return "";
      }
      for(var i=0;i<this.secondOrgList.length;i++){
        if(id == this.secondOrgList[i].orgId){
          return this.secondOrgList[i].orgName;
        }
      }
    },
    getFormData(){
      if(this.currentOrg.orgId==0){
        this.form = {}
      }else{
        getPlatform(this.currentOrg.orgId).then(response => {
          this.form = response.data;
        });
      }
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect({orgGroupOpt:"2"}).then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      //this.reset();
    },
    // 表单重置
    reset() {
      this.form = { };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实验平台信息";
    },
    /** 修改按钮操作 */
    handleUpdate() {
      getPlatform(this.currentOrg.orgId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改实验平台信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.platformId != null) {
            updatePlatform(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.$emit("refreshTree");
            });
          } else {
            addPlatform(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.$emit("refreshTree");
            });
          }
        }
      });
    },
  }
};
</script>
<style type="text/css" lang="scss" scoped>
  .input_w{
    width: 200px;
    margin-top: -2px;
  }
  .tb_edit{
  }
  .tb_edit tr td{
    padding: 10px 5px 10px 0px;
  }
  .td_text{
    text-align: left;
    valign: middle;
    width:200px;
  }
  .td_label{
    min-width:68px;
    valign:middle;
  }
</style>
